<?

/**
* This class should be extended to perform more validations.
* Usually the validate() method is the only one needed to be overwritten.
* Example:
* <code>
* $validator = new DataObjectValidator();
* $validator->setFields(array('nombre', 'email'));
* if (!$validator->validate($_POST))
* {
*     $validator->addErrorsToMessenger($messenger);
* }
* </code>
* @package database
*/

class DataObjectValidator extends Error
{

    public $_arFields = array();
    public $_arDefinedMsg = array(); // User defined error msg's:
    public $validateAllFields = false;
    public $doNotAllowEmptyFields = false;
    public $errors = array();
    public $showFieldsErrorMsg = true;

    public function __construct() {}

    /*********************
    ** $arMessages must be an array like this: array ("fieldName" => "mi defined error" , ... )
    ** Note:
    ** Actually only is usefull if DataObjectValidator::__validateField(...) don't found the $fieldName
    */
    public function redefineMessages($arMessages)
    {
        if (is_array($arMessages) && count($arMessages) > 0)
        {
            $this->_arDefinedMsg = array_merge($this->_arDefinedMsg, $arMessages);
            return 1;
        }
        return 0;
    }

    public function setFields($fields)
    {
        if (is_array($fields)) $this->_arFields = $fields;
    }

    public function validate($fieldsToValidate)
    {
        $errors=0;
        $numOfValidatedFields = 0;
        if ($this->validateAllFields===true)
        {
            foreach ($fieldsToValidate AS $field => $value)
            {
                $numOfValidatedFields++;
                if (!$this->_validateField($field, $value))
                    $errors++;
            }
            if ($numOfValidatedFields < count($fieldsToValidate))
                $errors++;
        }
        else
        {
            foreach ($this->_arFields AS $field)
            {
                $numOfValidatedFields++;
                if(isset($fieldsToValidate[$field]) && !$this->_validateField($field, $fieldsToValidate[$field]))
                    $errors++;
            }
            if ($numOfValidatedFields < count($this->_arFields))
                $errors++;
        }
        return $errors>0 ? 0 : 1;
    }

    protected function _validateField($fieldName, $fieldValue)
    {
        // Switch with the rules
        switch ($fieldName)
        {
            case 'email':
                if(!isRFC3696ValidEmailAddress($fieldValue)) $this->errors[$fieldName] = "Please complete the field Email.";
                break;
            default:
                if ($this->doNotAllowEmptyFields && !$fieldValue)
                {
                    $this->errors[$fieldName] = ($this->_arDefinedMsg[$fieldName] ? $this->_arDefinedMsg[$fieldName] : ('Please complete the field '. ucfirst(str_replace("_"," ",$fieldName)) .'.' ) );
                }
                break;
        }
        return isset($this->errors[$fieldName])? 0 : 1;
    }

    public function addErrorsToMessenger(&$messenger)
    {
        if (!is_a($messenger, 'Messenger'))
        {
            $this->_error('parameter 1 should be a Messenger Object');
            return 0;
        }

        foreach($this->errors AS $errormsg)
        {
            $messenger->addMessage($errormsg);
        }
    }

    public function showFieldErrorMsg($field)
    {
        if ($this->showFieldsErrorMsg && $this->errors[$field]) echo "<div class=\"fieldErrorMsg\">". $this->errors[$field] ."</div>";
    }
}
?>
